package cn.tedu.smartmarketing.user.service.impl;

import cn.tedu.smartmarketing.base.exception.ServiceException;
import cn.tedu.smartmarketing.base.response.StatusCode;
import cn.tedu.smartmarketing.user.mapper.UserMapper;
import cn.tedu.smartmarketing.user.pojo.dto.CommentSubmitParam;
import cn.tedu.smartmarketing.user.pojo.dto.OrderAdd;
import cn.tedu.smartmarketing.user.pojo.dto.UserLoginParam;
import cn.tedu.smartmarketing.user.pojo.dto.UserSaveParam;
import cn.tedu.smartmarketing.user.pojo.entity.Comment;
import cn.tedu.smartmarketing.user.pojo.entity.Order;
import cn.tedu.smartmarketing.user.pojo.entity.User;
import cn.tedu.smartmarketing.user.pojo.vo.ActivityVO;
import cn.tedu.smartmarketing.user.pojo.vo.UserVO;
import cn.tedu.smartmarketing.user.pojo.vo.UserVo1;
import cn.tedu.smartmarketing.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public List<ActivityVO> activityList(Integer businessId) {
        log.debug("查询活动业务：{}",businessId);
        List<ActivityVO> list= userMapper.activityList(businessId);
        log.debug("查询活动结果：{}",list);
        return list;
    }
    @Override
    public void reg(UserSaveParam userSaveParam) {
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        if (userSaveParam.getId()==null){
            String username = userSaveParam.getUsername();
            int num =  userMapper.selectByUsername(username);
            if (num>0){
                throw new ServiceException(StatusCode.USERNAME_ALREADY_EXISTS);
            }
            user.setCreateDate(new Date());
            user.setUpdateDate(new Date());
            userMapper.insert(user);
        }else {
            user.setUpdateDate(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        UserVO userVO = userMapper.login(userLoginParam.getUsername());
        if (userVO==null){
            throw  new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userLoginParam.getPassword().equals(userVO.getPassword())){
            throw  new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }

    @Override
    public void deleteUser(Integer userId) {
        log.debug("用户注销业务"+userId);
        User user = new User();
        user.setId(userId);
        user.setUpdateDate(new Date());
        userMapper.delete(user);
    }
    @Override
    public void submitComment(CommentSubmitParam commentSubmitParam) {
        log.debug("评论提交{}", commentSubmitParam);
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentSubmitParam,comment);
        comment.setCreateTime(new Date());
        userMapper.submitComment(comment);
    }

    @Override
    public void createOrder(OrderAdd orderAdd) {
        log.debug("实现类启动:{}", orderAdd);
        Order order = new Order();
        BeanUtils.copyProperties(orderAdd, order);
        order.setPaymentTime(new Date());
        order.setCreatedAt(new Date());
        order.setUpdatedAt(new Date());
        order.setOrderStatus(1);
        order.setPaymentStatus(1);
        userMapper.createOrder(order);
        log.debug("实现类成功");
    }

    @Override
    public List<UserVo1> listOrder(Integer userId) {
        log.debug("查询用户订单:{}", userId);
        return userMapper.listOrder(userId);
    }
}
